'use strict';

app.controller('viewMonthRepayCtrl', function($element, $scope, $state, rest, user, $rootScope, ngDialog, uiLoad, $timeout, $window, $ocLazyLoad, uploadConf, uploadprefix, tipService) {
    var data = angular.copy($scope.ngDialogData, {});
    var token = $window.localStorage.getItem("token");

    $scope.myData = {
        orderNo: data.orderNo,
        decorteOrderInfoId: data.id,
        planRepaymentDate: data.planRepaymentDate,
        periodNum: data.periodNum,
        planRepaymentAmount: data.planRepaymentAmount
    }
    $scope.queryParams = {
        decorteOrderInfoId: data.id,
        planRepaymentDate: data.planRepaymentDate,
        repaymentDate: "",
    };
    $scope.$on('msg2', function(e, msg) {
        $scope.confirm({});
    });

    $scope.column2 = 0; //分栏
    $scope.maxImgNum = 1; //最大图片数量


    $scope.imgNew = [{
        text: '请选择照片',
        name: 'choosePhoto',
        value: [],
    }];
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum = $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;

    $scope.cancelRepay = function() {
        $scope.closeThisDialog();
    }

    $scope.makeSureRepay = function() {
        var repaymentVoucher = $scope.imgNew[0].value;
        if ($scope.imgNew[0].value.length == 0) {
            tipService.show("body", {
                message: "请上传凭证",
                notice: "danger"
            }, 3);
            return;
        }

        rest.decoJava({
            type: "post",
            url: "/tenancy-sys/admin/decorateOrderInfo/manualPayment",
            data: {
                decorateOrderInfoId: $scope.myData.decorteOrderInfoId,
                repaymentDate: $scope.queryParams.repaymentDate,
                subtractOverdueFee: $scope.queryParams.installment,
                overdueFee: $scope.queryParams.overdueFee,
                tradeNo: $scope.queryParams.repayNo,
                repaymentVoucher: repaymentVoucher[0],
                remark: $scope.queryParams.remark
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(data) {
                if (data.code == "0000") {
                    $scope.$apply(function() {
                        $scope.queryParams.realRepaymentAmount = data.result.realRepaymentAmount;
                        $scope.queryParams.overdueFee = data.result.overdueFee;
                    })
                    $scope.$apply();
                    tipService.show("body", {
                        message: data.message,
                    }, 3);
                    $scope.closeThisDialog();
                } else {
                    tipService.show("body", {
                        message: data.message,
                        notice: "danger"
                    }, 3);
                }

            }
        });
    }

    $scope.$watch('queryParams.installment', function(newValue, oldValue) {
        if ($scope.queryParams.installment != null) {
            var repayMon = Number($scope.myData.planRepaymentAmount) + Number($scope.queryParams.overdueFee);
            $scope.queryParams.realRepaymentAmount = repayMon - Number(newValue);
            $scope.queryParams.realRepaymentAmount = $scope.queryParams.realRepaymentAmount.toFixed(2);
            console.log($scope.queryParams.realRepaymentAmount);
        }
    });

    $scope.$on('contractDate.start', function(event, data) {
        $scope.queryParams.repaymentDate = data;
        // console.log(data);
        rest.decoJava({
            type: "post",
            url: "/tenancy-sys/admin/decorateOrderInfo/countOverdueFee",
            data: {
                decorateRepaymentPlanId: $scope.myData.decorteOrderInfoId,
                planRepaymentDate: $scope.myData.planRepaymentDate,
                repaymentDate: data
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(data) {
                if (data.code == "0000") {
                    $scope.$apply(function() {
                        $scope.queryParams.realRepaymentAmount = data.result.realRepaymentAmount;
                        $scope.queryParams.overdueFee = data.result.overdueFee;
                    })
                    $scope.$apply();

                } else {
                    tipService.show("body", {
                        message: data.message,
                        notice: "danger"
                    }, 3);
                }

            }
        });
    });

});

app.controller('ContractTimeCtrl', function($scope) {
    $scope.today = function() {
        $scope.contractStartDate = "";
    };

    $scope.closeAll = function() {
        $scope.openContractBeginDated =
            $scope.openContractEndDated = false;
    }

    $scope.openContractBeginDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openContractBeginDated = true;
    };

    $scope.dateOptions = {
        format: 'yyyy-MM-dd',
        startingDay: 1,
        class: 'datepicker'
    };

    $scope.format = 'yyyy-MM-dd';

    $scope.today();

    $scope.$watch('contractStartDate', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.start', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.start', '')
        }
    });

});